package com.boot.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

//将异常处理统一存放到一个处理器中
//@ControllerAdvice //用来存放异常处理的
public class ErrorHandler {


    /**
     * 自定义异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(value = {NullPointerException.class,ArithmeticException.class})
    public ModelAndView ex1(Exception e){
        ModelAndView mv = new ModelAndView("myError");
        mv.addObject("errorMsg","异常信息:"+e.getMessage());
        return mv;
    }

    /**
     * 可以定义多个ExceptionHandler
     * @param e
     * @return
     */
    @ExceptionHandler(value = {ArrayIndexOutOfBoundsException.class})
    public ModelAndView ex2(Exception e){
        ModelAndView mv = new ModelAndView("myError");
        mv.addObject("errorMsg","数组越界异常信息:"+e.getMessage());
        return mv;
    }

}
